﻿using Repository.MODELs;
using System;
using System.Linq;
using Windows.UI.Notifications;

namespace Repository
{
    class NotificationUtils
    {
        public static void CreateNotification(TaskModel data)
        {
            // NghiaTT - Add toast notification
            // TODO: test

            //const string xmlFormat = "<toast><visual><binding template='ToastText02'><image src='../Assets/newSmallLogo.png'></image><text id='1'>{0}</text><text id='2'>{1}</text></binding></visual></toast>";
            //var xmlString = string.Format(xmlFormat, data.Name, data.Description);
            //var xmlDom = new XmlDocument();
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
                var textNodes = toastXml.GetElementsByTagName("text");
                textNodes[0].AppendChild(toastXml.CreateTextNode(data.Name + string.Empty));
                textNodes[1].AppendChild(toastXml.CreateTextNode(data.Description + string.Empty));

                var audio = toastXml.CreateElement("audio");
                audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Alarm");
                audio.SetAttribute("loop", "true");

                var dueTime = RepositoryUtils.GetDateTimeFromStrings(data.StartDate, data.StartTime).AddSeconds(15); // 15' before the start time

                if (dueTime < DateTime.Now) return;
                //var dueTime = DateTime.Now.AddSeconds(5);
                ScheduledToastNotification toast;

                switch (data.RepeatType)
                {
                    case 1:
                        toast = new ScheduledToastNotification(toastXml, dueTime, TimeSpan.FromDays(1), 5);
                        break;
                    case 2:
                        toast = new ScheduledToastNotification(toastXml, dueTime, TimeSpan.FromDays(7), 5);
                        break;
                    case 3:
                        // TODO: check for day in month
                        toast = new ScheduledToastNotification(toastXml, dueTime, TimeSpan.FromDays(30), 5);
                        break;
                    case 4:
                        // TODO: check for leap year
                        toast = new ScheduledToastNotification(toastXml, dueTime, TimeSpan.FromDays(365), 5);
                        break;
                    default:
                        toast = new ScheduledToastNotification(toastXml, dueTime);
                        break;
                }
                toast.Id = data.ID.ToString();
                ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);
            }
            catch { }
        }

        public static void DeleteNotification(int id)
        {
            try
            {
                var itemId = id.ToString();
                var notifier = ToastNotificationManager.CreateToastNotifier();
                var scheduled = notifier.GetScheduledToastNotifications();
                foreach (var t in scheduled.Where(t => t.Id.Equals(itemId)))
                {
                    notifier.RemoveFromSchedule(t);
                }
            }
            catch { }
        }
    }
}
